package com.cweb.framework.generator.gen;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.cweb.framework.generator.GenConstant;
import com.cweb.framework.generator.gen.config.GeneratorSetting;
import com.cweb.framework.generator.tableInfo.entity.ColumnInfo;
import com.cweb.framework.generator.tableInfo.entity.TableInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GeneratorUtil {
    private static final Log log = LogFactory.get();

    // 基本路径
    public final static String SETTING_BASE_VM_PATH = "generator/vm";

    /**
     * 类型转换
     */
    private static Map<String, String> javaTypeMap = new HashMap<String, String>();

    static {
        // 初始化 - 类型转换
        javaTypeMap.put("tinyint", "Integer");
        javaTypeMap.put("smallint", "Integer");
        javaTypeMap.put("mediumint", "Integer");
        javaTypeMap.put("int", "Integer");
        javaTypeMap.put("integer", "integer");
        javaTypeMap.put("bigint", "Long");
        javaTypeMap.put("float", "Float");
        javaTypeMap.put("double", "Double");
        javaTypeMap.put("decimal", "BigDecimal");
        javaTypeMap.put("bit", "Boolean");
        javaTypeMap.put("char", "String");
        javaTypeMap.put("varchar", "String");
        javaTypeMap.put("tinytext", "String");
        javaTypeMap.put("text", "String");
        javaTypeMap.put("mediumtext", "String");
        javaTypeMap.put("longtext", "String");
        javaTypeMap.put("time", "Date");
        javaTypeMap.put("date", "Date");
        javaTypeMap.put("datetime", "Date");
        javaTypeMap.put("timestamp", "Date");
    }

    /**
     * 取得模板列表
     */
    public static List<String> getTemplatesList(GeneratorSetting setting) {
        List<String> templatesList = new ArrayList<String>();
        templatesList.add("/java/Entity.java.vm");
        templatesList.add("/java/Mapper.java.vm");
        templatesList.add("/java/Service.java.vm");
        templatesList.add("/java/Controller.java.vm");
        templatesList.add("/xml/Mapper.xml.vm");
        templatesList.add("/vue/Manager.vue.vm");
        if ("zip".equals(setting.getGeneratorType())) {
            templatesList.add("/text/Readme.txt.vm");
        }
        return templatesList;
    }
    /**
     * 表名转换成Java类名
     */
    public static String tableToJava(String tableName) {
        return StrUtil.toCamelCase(tableName);
    }
    /**
     * 获取java文件基本路径
     */
    public static String getJavaPath(TableInfo table, GeneratorSetting setting) {
        // java的基本路径
        String javaPath = String.format("com.%s.%s.model.", setting.getProjectKey(), setting.getProjectName());
        if (StrUtil.isNotBlank(setting.getModule())) {
            javaPath = javaPath + StrUtil.nullToDefault(setting.getModule(), "") + "." ;
        }
        javaPath = javaPath+ table.getTableName() + "." ;
        return javaPath;
    }
    /**
     * 获取文件名
     */
    public static String getFileName(String templateName, TableInfo table, GeneratorSetting setting) {
        // 大写类名
        String className = StrUtil.upperFirst(table.getClassName());
        // java的基本路径
        String javaPath = GenConstant.PROJECT_JAVA_BASE_PATH + getJavaPath(table, setting).replaceAll("\\.","/");
        // vue的基本路径
        String vuePath = GenConstant.PROJECT_VUE_BASE_PATH + setting.getModule();

        // java路径转换
        // 后端部分
        if (templateName.contains("Entity.java.vm")) {
            return String.format("%sentity/%s.java", javaPath, className);
        }
        if (templateName.contains("Mapper.java.vm")) {
            return String.format("%smapper/%sMapper.java", javaPath, className);
        }
        if (templateName.contains("Service.java.vm")) {
            return String.format("%sservice/%sService.java", javaPath, className);
        }
        if (templateName.contains("Controller.java.vm")) {
            return String.format("%scontroller/%sController.java", javaPath, className);
        }
        if (templateName.contains("Mapper.xml.vm")) {
            return String.format("%sxml/%sMapper.xml", javaPath, className);
        }
        // 前端部分
        if (templateName.contains("Manager.vue.vm")) {
            return String.format("%s/%sManager.vue", vuePath, className);
        }
        // 帮助文档
        if (templateName.contains("Readme.txt.vm")) {
            return "Readme.txt" ;
        }
        return null;
    }
    /**
     * 设置列信息
     */
    public static List<ColumnInfo> transColums(List<ColumnInfo> columns) {
        // 列信息
        List<ColumnInfo> columsList = new ArrayList<>();
        for (ColumnInfo column : columns) {
            // 列名转换成Java属性名
            String attrName = StrUtil.toCamelCase(column.getColumnName());
            column.setAttrName(attrName);
            column.setAttrNameUpper(StrUtil.upperFirst(attrName));
            column.setColumnNameShort(column.getColumnName().substring(column.getColumnName().indexOf("_") + 1));

            // 列的数据类型，转换成Java类型
            String attrType = javaTypeMap.get(column.getDataType());
            column.setAttrType(attrType);

            columsList.add(column);
        }
        return columsList;
    }
}
